unit uDBData;

interface

uses
  SysUtils, Classes, DB, MemDS, DBAccess, MyAccess, Graphics, ImageTools,
  frxClass, frxDBSet, frxDesgn, frxDACComponents, frxMYDACComponents, frxCustomDBRTTI,
  ImgList, Controls;

type
  TfDBData = class(TDataModule)
    qWindow: TMyQuery;
    qWindowGroup: TMyQuery;
    qWindowGroupname: TWideStringField;
    dWindow: TMyDataSource;
    dWindowGroup: TMyDataSource;
    qOffice: TMyQuery;
    dOffice: TMyDataSource;
    qApartmentPlanning: TMyQuery;
    dApartmentPlanning: TMyDataSource;
    qWindowType: TMyQuery;
    qWindowProfile: TMyQuery;
    dWindowType: TMyDataSource;
    dWindowProfile: TMyDataSource;
    qAgreement: TMyQuery;
    qAgreementPayment: TMyQuery;
    qAgreementWindow: TMyQuery;
    qAgreementWindowSizePosition: TMyQuery;
    qAgreementWindowMaterial: TMyQuery;
    qAgreementWorkman: TMyQuery;
    qAgreementWindowOption: TMyQuery;
    qWindowOption: TMyQuery;
    dWindowOption: TMyDataSource;
    dAgreement: TMyDataSource;
    dAgreementPayment: TMyDataSource;
    dAgreementWindow: TMyDataSource;
    dAgreementWindowSizePosition: TMyDataSource;
    dAgreementWindowMaterial: TMyDataSource;
    dAgreementWorkman: TMyDataSource;
    dAgreementWindowOption: TMyDataSource;
    qMaterial: TMyQuery;
    dMaterial: TMyDataSource;
    qSizePosition: TMyQuery;
    dSizePosition: TMyDataSource;
    qWorkman: TMyQuery;
    dWorkman: TMyDataSource;
    qWorkmanGroup: TMyQuery;
    dWorkmanGroup: TMyDataSource;
    qWorkmanSalary: TMyQuery;
    dWorkmanSalary: TMyDataSource;
    qWorkmanSalaryDetail: TMyQuery;
    dWorkmanSalaryDetail: TMyDataSource;
    qApartmentWindowMaterial: TMyQuery;
    dApartmentWindowMaterial: TMyDataSource;
    qAdvertismentMoney: TMyQuery;
    dAdvertismentMoney: TMyDataSource;
    qWorkmanConsuption: TMyQuery;
    dWorkmanConsuption: TMyDataSource;
    qApartmentWindowWorkmanSalary: TMyQuery;
    dApartmentWindowWorkmanSalary: TMyDataSource;
    qMoneyOperation: TMyQuery;
    dMoneyOperation: TMyDataSource;
    qAgreementPaymentsumma: TFloatField;
    qAgreementWindowSizePositionvalue: TWideStringField;
    qAgreementWindowMaterialvalue: TWideStringField;
    qAgreementWindowOptionxpos: TIntegerField;
    qAgreementWindowOptionypos: TIntegerField;
    qSizePositionxpos: TIntegerField;
    qSizePositionox: TBooleanField;
    qSizePositionypos: TIntegerField;
    qSizePositionoy: TBooleanField;
    qSizePositionangle: TIntegerField;
    qWorkmanConsuptionsumma: TFloatField;
    qAgreementWindowOtliv: TMyQuery;
    dAgreementWindowOtliv: TMyDataSource;
    qAgreementWindowOtlivSizePosition: TMyQuery;
    dAgreementWindowOtlivSizePosition: TMyDataSource;
    qOtliv: TMyQuery;
    dOtliv: TMyDataSource;
    qOtlivGroup: TMyQuery;
    dOtlivGroup: TMyDataSource;
    qOtlivGroupSizePosition: TMyQuery;
    dOtlivGroupSizePosition: TMyDataSource;
    qApartmentWindowMaterialvalue: TFloatField;
    qWindowGroupSizePosition: TMyQuery;
    dWindowGroupSizePosition: TMyDataSource;
    qApartmentWindowWorkmanSalaryvalue: TFloatField;
    qApartmentPlanningname: TWideStringField;
    qOfficename: TWideStringField;
    qWindowTypename: TWideStringField;
    qWindowProfilename: TWideStringField;
    qWindowWindowGroupName: TWideStringField;
    qAgreementsumma: TFloatField;
    qAgreementOfficeName: TWideStringField;
    qAgreementApartmentPlanningName: TWideStringField;
    qMaterialname: TWideStringField;
    qSizePositionname: TWideStringField;
    qWorkmanGroupname: TWideStringField;
    qAdvertismentMoneysumma: TFloatField;
    qMoneyOperationname: TWideStringField;
    qAgreementWindowOtlivamount: TIntegerField;
    qAgreementWindowOtlivSizePositionvalue: TWideStringField;
    qOtlivGroupname: TWideStringField;
    qReport: TMyQuery;
    dReport: TMyDataSource;
    qWindowGroupSizePositionSizePositionName: TWideStringField;
    qApartmentWindowMaterialMaterialName: TWideStringField;
    frxMainDesigner: TfrxDesigner;
    qAgreementWindowMaterialMaterialName: TWideStringField;
    qWorkmanWorkmanGroupName: TWideStringField;
    qWorkmanConsuptionMoneyOperationName: TWideStringField;
    qOtlivOtlivGroupName: TWideStringField;
    qAgreementWindowSizePositionSizePositionName: TWideStringField;
    qAgreementWindowOtlivSizePositionSizePositionName: TWideStringField;
    qAgreementWorkmanWorkmanFirstName: TWideStringField;
    qAgreementWorkmanWorkmanSecondName: TWideStringField;
    qAgreementWorkmanWorkmanLastName: TWideStringField;
    qAgreementWorkmanWorkmanGroupName: TWideStringField;
    frxMainReport: TfrxReport;
    frxOffice: TfrxDBDataset;
    frxApartmentPlanning: TfrxDBDataset;
    frxWindowType: TfrxDBDataset;
    frxWindowProfile: TfrxDBDataset;
    frxWindow: TfrxDBDataset;
    frxWindowGroup: TfrxDBDataset;
    frxWindowOption: TfrxDBDataset;
    frxAgreement: TfrxDBDataset;
    frxAgreementPayment: TfrxDBDataset;
    frxAgreementWindow: TfrxDBDataset;
    frxAgreementWindowSizePosition: TfrxDBDataset;
    frxAgreementWindowMaterial: TfrxDBDataset;
    frxAgreementWorkman: TfrxDBDataset;
    frxAgreementWindowOption: TfrxDBDataset;
    frxMaterial: TfrxDBDataset;
    frxSizePosition: TfrxDBDataset;
    frxWorkman: TfrxDBDataset;
    frxWorkmanGroup: TfrxDBDataset;
    frxWorkmanSalary: TfrxDBDataset;
    frxWorkmanSalaryDetail: TfrxDBDataset;
    frxApartmentWindowMaterial: TfrxDBDataset;
    frxAdvertismentMoney: TfrxDBDataset;
    frxWorkmanConsuption: TfrxDBDataset;
    frxApartmentWindowWorkmanSalary: TfrxDBDataset;
    frxMoneyOperation: TfrxDBDataset;
    frxAgreementWindowOtliv: TfrxDBDataset;
    frxAgreementWindowOtlivSizePosition: TfrxDBDataset;
    frxOtliv: TfrxDBDataset;
    frxOtlivGroup: TfrxDBDataset;
    frxOtlivGroupSizePosition: TfrxDBDataset;
    frxReport: TfrxDBDataset;
    frxWindowGroupSizePosition: TfrxDBDataset;
    qAgreementStreet: TWideStringField;
    qAgreementHome: TWideStringField;
    qAgreementApartment: TWideStringField;
    qAgreementEntrance: TWideStringField;
    qAgreementFloor: TWideStringField;
    qClient: TMyQuery;
    dClient: TMyDataSource;
    frxClient: TfrxDBDataset;
    qWindowOptionMaterialName: TWideStringField;
    ImageList24x24: TImageList;
    qOfficeIsDeleted: TBooleanField;
    qApartmentPlanningIsDeleted: TBooleanField;
    qWindowTypeIsDeleted: TBooleanField;
    qWindowProfileIsDeleted: TBooleanField;
    qWindowWindowGroupId: TIntegerField;
    qWindowWindowImage: TBlobField;
    qWindowIsDeleted: TBooleanField;
    qWindowGroupIsDeleted: TBooleanField;
    qWindowOptionMaterialId: TIntegerField;
    qWindowOptionOptionImage: TBlobField;
    qWindowOptionIsDeleted: TBooleanField;
    qMaterialIsMaterial: TBooleanField;
    qMaterialIsPodokonnik: TBooleanField;
    qMaterialIsOtliv: TBooleanField;
    qMaterialIsDeleted: TBooleanField;
    qAgreementClientId: TIntegerField;
    qAgreementWorkmanId: TIntegerField;
    qAgreementOfficeId: TIntegerField;
    qAgreementApartmentPlanningId: TIntegerField;
    qAgreementIsSelected: TBooleanField;
    qAgreementIsDeleted: TBooleanField;
    qAgreementDataFolder: TDateField;
    qAgreementFirstName: TWideStringField;
    qAgreementSecondName: TWideStringField;
    qAgreementLastName: TWideStringField;
    qAgreementPhoneHome: TMemoField;
    qAgreementPhoneMobile: TMemoField;
    qOfficeOfficeId: TIntegerField;
    qApartmentPlanningApartmentPlanningId: TIntegerField;
    qWindowTypeWindowTypeId: TIntegerField;
    qWindowProfileWindowProfileId: TIntegerField;
    qWindowGroupWindowGroupId: TIntegerField;
    qWindowWindowId: TIntegerField;
    qMaterialMaterialId: TIntegerField;
    qWindowOptionWindowOptionId: TIntegerField;
    qAgreementAgreementId: TIntegerField;
    qAgreementPaymentAgreementPaymentId: TIntegerField;
    qAgreementPaymentAgreementId: TIntegerField;
    qAgreementPaymentIsDeleted: TBooleanField;
    qAgreementWindowAgreementWindowId: TIntegerField;
    qAgreementWindowAgreementId: TIntegerField;
    qAgreementWindowWindowId: TIntegerField;
    qAgreementWindowWindowTypeId: TIntegerField;
    qAgreementWindowWindowProfileId: TIntegerField;
    qAgreementWindowIsDeleted: TBooleanField;
    qAgreementWindowSizePositionAgreementWindowSizePositionId: TIntegerField;
    qAgreementWindowSizePositionAgreementWindowId: TIntegerField;
    qAgreementWindowSizePositionSizePositionId: TIntegerField;
    qAgreementWindowSizePositionIsDeleted: TBooleanField;
    qSizePositionSizePositionId: TIntegerField;
    qSizePositionIsDeleted: TBooleanField;
    qAgreementWindowMaterialAgreementWindowMaterialId: TIntegerField;
    qAgreementWindowMaterialAgreementWindowId: TIntegerField;
    qAgreementWindowMaterialMaterialId: TIntegerField;
    qAgreementWindowMaterialIsDeleted: TBooleanField;
    qAgreementWindowMaterialIsPodokonnik: TBooleanField;
    qAgreementWindowMaterialIsOtliv: TBooleanField;
    qAgreementWorkmanAgreementWorkmanId: TIntegerField;
    qAgreementWorkmanAgreementId: TIntegerField;
    qAgreementWorkmanWorkmanId: TIntegerField;
    qAgreementWorkmanIsDeleted: TBooleanField;
    qWorkmanWorkmanId: TIntegerField;
    qWorkmanWorkmanGroupId: TIntegerField;
    qWorkmanFirstName: TWideStringField;
    qWorkmanSecondName: TWideStringField;
    qWorkmanLastName: TWideStringField;
    qWorkmanIsDeleted: TBooleanField;
    qAgreementWindowOptionAgreementWindowOptionId: TIntegerField;
    qAgreementWindowOptionAgreementWindowId: TIntegerField;
    qAgreementWindowOptionAgreementWindowMaterialId: TIntegerField;
    qAgreementWindowOptionWindowOptionId: TIntegerField;
    qAgreementWindowOptionIsDeleted: TBooleanField;
    qClientClientId: TIntegerField;
    qClientPhoneHome: TMemoField;
    qClientPhoneMobile: TMemoField;
    qClientIsDeleted: TBooleanField;
    qWorkmanGroupWorkmanGroupId: TIntegerField;
    qWorkmanGroupIsDeleted: TBooleanField;
    qApartmentWindowMaterialApartmentWindowMaterialId: TIntegerField;
    qApartmentWindowMaterialApartmentPlanningId: TIntegerField;
    qApartmentWindowMaterialWindowGroupId: TIntegerField;
    qApartmentWindowMaterialMaterialId: TIntegerField;
    qApartmentWindowMaterialIsDeleted: TBooleanField;
    qAdvertismentMoneyAdvertismentMoneyId: TIntegerField;
    qAdvertismentMoneyAgreementId: TIntegerField;
    qAdvertismentMoneyIsDeleted: TBooleanField;
    qMoneyOperationMoneyOperationId: TIntegerField;
    qMoneyOperationIsGeneral: TBooleanField;
    qMoneyOperationIsDeleted: TBooleanField;
    qWorkmanConsuptionWorkmanConsuptionId: TIntegerField;
    qWorkmanConsuptionWorkmanId: TIntegerField;
    qWorkmanConsuptionMoneyOperationId: TIntegerField;
    qWorkmanConsuptionIsDeleted: TBooleanField;
    qApartmentWindowWorkmanSalaryApartmentWindowWorkmanSalaryId: TIntegerField;
    qApartmentWindowWorkmanSalaryApartmentPlanningId: TIntegerField;
    qApartmentWindowWorkmanSalaryWindowGroupId: TIntegerField;
    qApartmentWindowWorkmanSalaryWorkmanGroupId: TIntegerField;
    qApartmentWindowWorkmanSalaryIsDeleted: TBooleanField;
    qOtlivOtlivId: TIntegerField;
    qOtlivOtlivGroupId: TIntegerField;
    qOtlivOtlivImage: TBlobField;
    qOtlivIsDeleted: TBooleanField;
    qOtlivGroupOtlivGroupId: TIntegerField;
    qOtlivGroupIsDeleted: TBooleanField;
    qAgreementWindowOtlivAgreementWindowOtlivId: TIntegerField;
    qAgreementWindowOtlivAgreementWindowId: TIntegerField;
    qAgreementWindowOtlivOtlivId: TIntegerField;
    qAgreementWindowOtlivIsDeleted: TBooleanField;
    qAgreementWindowOtlivSizePositionAgreementWindowOtlivSizePositionId: TIntegerField;
    qAgreementWindowOtlivSizePositionAgreementWindowOtlivId: TIntegerField;
    qAgreementWindowOtlivSizePositionSizePositionId: TIntegerField;
    qAgreementWindowOtlivSizePositionIsDeleted: TBooleanField;
    qOtlivGroupSizePositionOtlivGroupSizePositionId: TIntegerField;
    qOtlivGroupSizePositionOtlivGroupId: TIntegerField;
    qOtlivGroupSizePositionSizePositionId: TIntegerField;
    qOtlivGroupSizePositionIsDeleted: TBooleanField;
    qReportReportId: TIntegerField;
    qReportName: TWideStringField;
    qReportFileName: TWideStringField;
    qReportIsDeleted: TBooleanField;
    qWindowGroupSizePositionWindowGroupSizePositionId: TIntegerField;
    qWindowGroupSizePositionWindowGroupId: TIntegerField;
    qWindowGroupSizePositionSizePositionId: TIntegerField;
    qWindowGroupSizePositionIsDeleted: TBooleanField;
    qAgreementNumber: TWideStringField;
    qAgreementWindowWindowTypeName: TWideStringField;
    qAgreementWindowWindowProfileName: TWideStringField;
    qAgreementOperationParamColor: TMyQuery;
    dAgreementOperationParamColor: TMyDataSource;
    qOperationParamColor: TMyQuery;
    dOperationParamColor: TMyDataSource;
    qAgreementWindowOperationParamColor: TMyQuery;
    dAgreementWindowOperationParamColor: TMyDataSource;
    qAgreementOperationParamColorAgreementOperationParamColorId: TIntegerField;
    qAgreementOperationParamColorAgreementId: TIntegerField;
    qAgreementOperationParamColorOperationParamColorId: TIntegerField;
    qAgreementOperationParamColorIsDeleted: TBooleanField;
    qAgreementOperationParamColorIsFinished: TBooleanField;
    qAgreementOperationParamColorOperationParamColorName: TWideStringField;
    qOperationParamColorOperationParamColorId: TIntegerField;
    qOperationParamColorName: TWideStringField;
    qOperationParamColorFontColor: TWideStringField;
    qOperationParamColorBGColor: TWideStringField;
    qOperationParamColorIsDeleted: TBooleanField;
    qOperationParamColorIsVisibled: TBooleanField;
    qAgreementWindowOperationParamColorAgreementWindowOperationParamColorId: TIntegerField;
    qAgreementWindowOperationParamColorAgreementWindowId: TIntegerField;
    qAgreementWindowOperationParamColorOperationParamColorId: TIntegerField;
    qAgreementWindowOperationParamColorIsFinished: TBooleanField;
    qAgreementWindowOperationParamColorIsDeleted: TBooleanField;
    qAgreementWindowOperationParamColorOperationParamColorName: TWideStringField;
    qAgreementAData: TDateField;
    qAgreementPaymentAData: TDateField;
    qWorkmanConsuptionAData: TDateField;
    qAdvertismentMoneyAData: TDateField;
    qClientFirstName: TWideStringField;
    qClientSecondName: TWideStringField;
    qClientLastName: TWideStringField;
    qClientStreet: TWideStringField;
    qClientHome: TWideStringField;
    qClientApartment: TWideStringField;
    qClientEntrance: TWideStringField;
    qClientFLOOR: TWideStringField;
    qOtlivGroupSizePositionSizePositionName: TStringField;
    qWorkmanSalaryWorkmanId: TIntegerField;
    qWorkmanSalaryName: TWideStringField;
    qWorkmanSalaryFirstName: TWideStringField;
    qWorkmanSalarySecondName: TWideStringField;
    qWorkmanSalaryLastName: TWideStringField;
    qWorkmanSalarysumma: TFloatField;
    qWorkmanSalaryDetailAgreementId: TIntegerField;
    qWorkmanSalaryDetailNumber: TWideStringField;
    qWorkmanSalaryDetailOfficeId: TIntegerField;
    qWorkmanSalaryDetailWindowCount: TLargeintField;
    qWorkmanSalaryDetailSumma: TFloatField;
    qWorkmanSalaryDetailADATA: TDateField;
    qWorkmanSalaryDetailOfficeName: TStringField;
    procedure qAgreementWindowMaterialAfterPost(DataSet: TDataSet);
    procedure qAgreementWindowMaterialBeforePost(DataSet: TDataSet);
    procedure qAgreementWindowMaterialAfterEdit(DataSet: TDataSet);
    procedure qWindowGroupSizePositionBeforePost(DataSet: TDataSet);
    procedure qApartmentPlanningAfterScroll(DataSet: TDataSet);
    procedure qApartmentWindowMaterialBeforePost(DataSet: TDataSet);
    procedure qWorkmanGroupAfterScroll(DataSet: TDataSet);
    procedure qWorkmanSalaryAfterScroll(DataSet: TDataSet);
    procedure qWorkmanConsuptionBeforePost(DataSet: TDataSet);
    procedure qOtlivGroupAfterScroll(DataSet: TDataSet);
    procedure qOtlivGroupSizePositionBeforePost(DataSet: TDataSet);
    procedure qApartmentWindowWorkmanSalaryBeforePost(
      DataSet: TDataSet);
    procedure qAgreementAfterScroll(DataSet: TDataSet);
    procedure qAgreementWorkmanBeforePost(DataSet: TDataSet);
    procedure qAgreementWindowAfterScroll(DataSet: TDataSet);
    procedure qWindowGroupAfterScroll(DataSet: TDataSet);
    function frxMainReportUserFunction(const MethodName: String;
      var Params: Variant): Variant;
    procedure qAgreementPaymentBeforePost(DataSet: TDataSet);
    procedure DataModuleCreate(Sender: TObject);
    procedure qAgreementOperationParamColorBeforePost(DataSet: TDataSet);
    procedure qAgreementWindowOperationParamColorBeforePost(
      DataSet: TDataSet);
    procedure qAgreementWindowOtlivAfterScroll(DataSet: TDataSet);
    procedure qAgreementWindowOtlivBeforePost(DataSet: TDataSet);
    procedure frxMainReportProgressStart(Sender: TfrxReport;
      ProgressType: TfrxProgressType; Progress: Integer);
    procedure frxMainReportProgressStop(Sender: TfrxReport;
      ProgressType: TfrxProgressType; Progress: Integer);
    procedure qWorkmanBeforePost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    is_Agreement, is_AgreementON: Boolean;
    function InitDBData():Boolean;
    procedure RefreshDataSet(DataSource: TDataSource);
    procedure InsertRecord(DataSource: TDataSource);
    procedure DeleteRecord(DataSource: TDataSource);
    procedure GetResults();
    function GetWindowImage():TBitmap;
    function GetAllWindowImage():TBitmap;
    function GetOtlivImage():TBitmap;
    function GetAllOtlivImage():TBitmap;
    function GetAllPodokonnik():String;
    function GetAllOtliv():String;
    function GetWindowMaterial(IsAllMaterial: Boolean):String;
    function GetAgreementPayment():Real;
  end;

var
  fDBData: TfDBData;

implementation

uses uDBCustom, uWindowImage, uDBCommand, Variants;

{$R *.dfm}

function ConvertToInt(Value : String) : Integer;
var
  Res, Ret, ErrorCode : Integer;
begin
  Ret := 0;
  if(Value <> '') then begin
    val(Value, Res, ErrorCode);
    if((Res > 0)and(ErrorCode = 0)) then
      Ret := Res
    else
      Ret := 0;
  end;
  Result := Ret;
end;

function TfDBData.InitDBData():Boolean;
begin
  if(fDBCustom.MyConnect.Connected) then
  begin
    qClient.Active := True;
    qOperationParamColor.Active := True;
    qSizePosition.Active := True;
    qOtlivGroup.Active := True;
    qOtliv.Active := True;
    qMaterial.Active := True;
    qWindowGroup.Active := True;
    qApartmentPlanning.Active := True;
    qApartmentWindowMaterial.Active := True;
    qWindow.Active := True;
    qWindowProfile.Active := True;
    qWindowType.Active := True;
    qMoneyOperation.Active := True;
    qWorkmanGroup.Active := True;
    qWorkman.Active := True;
    qWindowOption.Active := True;
    qReport.Active := True;
  end;
  Result := fDBCustom.MyConnect.Connected;
end;

procedure TfDBData.RefreshDataSet(DataSource: TDataSource);
begin
  if(DataSource.DataSet.Active) then
  begin
    DataSource.DataSet.Close();
    DataSource.DataSet.Open();
  end;
end;

procedure TfDBData.InsertRecord(DataSource: TDataSource);
begin
  if(DataSource.DataSet.Active) then
  begin
    DataSource.DataSet.Append();
  end;
end;

procedure TfDBData.DeleteRecord(DataSource: TDataSource);
begin
  if(DataSource.DataSet.IsEmpty = False) then
  begin
    DataSource.DataSet.Delete();
  end;
end;

function TfDBData.GetWindowImage():TBitmap;
var
  WindowSize: array of TWindowSize;
  amybitmap, bmybitmap, cmybitmap: TBitmap;
  CurRec, tx, ty: Integer;
begin
  amybitmap := TBitmap.Create();
  bmybitmap := TBitmap.Create();
  bmybitmap.Assign(HImageTools.GetBitmapByID(fDBData.dWindow, 'WindowId', fDBData.dAgreementWindow.DataSet.FieldByName('WindowId').AsString, 'WindowImage'));
  if(fDBData.dAgreementWindowSizePosition.DataSet.IsEmpty = False)then
  begin
    SetLength(WindowSize, fDBData.dAgreementWindowSizePosition.DataSet.RecordCount);
    fDBData.dAgreementWindowSizePosition.DataSet.First();
    while(fDBData.dAgreementWindowSizePosition.DataSet.Eof = False)do
    begin
      CurRec := fDBData.dAgreementWindowSizePosition.DataSet.RecNo;
      fDBData.dSizePosition.DataSet.Filtered := False;
      fDBData.dSizePosition.DataSet.Filter := Format('SizePositionId = %s', [fDBData.dAgreementWindowSizePosition.DataSet.FieldByName('SizePositionId').AsString]);
      fDBData.dSizePosition.DataSet.Filtered := True;
      WindowSize[CurRec-1].xpos := fDBData.dSizePosition.DataSet.FieldByName('xpos').AsInteger;
      WindowSize[CurRec-1].ox := fDBData.dSizePosition.DataSet.FieldByName('ox').AsBoolean;
      WindowSize[CurRec-1].ypos := fDBData.dSizePosition.DataSet.FieldByName('ypos').AsInteger;
      WindowSize[CurRec-1].oy := fDBData.dSizePosition.DataSet.FieldByName('oy').AsBoolean;
      WindowSize[CurRec-1].angle := fDBData.dSizePosition.DataSet.FieldByName('angle').AsInteger;
      WindowSize[CurRec-1].text := fDBData.dAgreementWindowSizePosition.DataSet.FieldByName('value').AsString;
      fDBData.dSizePosition.DataSet.Filtered := False;
      fDBData.dSizePosition.DataSet.Filter := '';
      fDBData.dAgreementWindowSizePosition.DataSet.Next();
    end;//end while
  end;//end if
  HImageTools.DrawRotateText(bmybitmap, amybitmap, WindowSize);
  bmybitmap.Free();
  if(fDBData.dAgreementWindowOption.DataSet.IsEmpty = False)then
  begin
    fDBData.dAgreementWindowOption.DataSet.First();
    while(fDBData.dAgreementWindowOption.DataSet.Eof = False)do
    begin
      cmybitmap := TBitmap.Create();
      cmybitmap.Assign(HImageTools.GetBitmapByID(fDBData.dWindowOption, 'WindowOptionId', fDBData.dAgreementWindowOption.DataSet.FieldByName('WindowOptionId').AsString, 'OptionImage'));
      tx := fDBData.dAgreementWindowOption.DataSet.FieldByName('xpos').AsInteger - Round(cmybitmap.Width/2);
      ty := fDBData.dAgreementWindowOption.DataSet.FieldByName('ypos').AsInteger - Round(cmybitmap.Height/2);
      cmybitmap.TransparentColor := clFuchsia;
      cmybitmap.Transparent := True;
      amybitmap.Canvas.Draw(tx, ty, cmybitmap);
      cmybitmap.Free();
      fDBData.dAgreementWindowOption.DataSet.Next();
    end;//end while
  end;//end if
  HImageTools.DefBitmapSizeIfEmpty(amybitmap);
  Result := amybitmap;
end;

function TfDBData.GetOtlivImage():TBitmap;
var
  WindowSize: array of TWindowSize;
  amybitmap, bmybitmap, cmybitmap: TBitmap;
  CurRec: Integer;
begin
  amybitmap := TBitmap.Create();
  bmybitmap := TBitmap.Create();
  cmybitmap := TBitmap.Create();
  if(fDBData.dAgreementWindowOtliv.DataSet.IsEmpty = False)then
  begin
    amybitmap.Assign(HImageTools.GetBitmapByID(fDBData.dOtliv, 'OtlivId', fDBData.dAgreementWindowOtliv.DataSet.FieldByName('OtlivId').AsString, 'OtlivImage'));
    amybitmap.TransparentColor := clFuchsia;
    amybitmap.Transparent := True;
    if(fDBData.dAgreementWindowOtlivSizePosition.DataSet.IsEmpty = False)then
    begin
      //SetLength(WindowSize, fDBData.dAgreementWindowOtlivSizePosition.DataSet.RecordCount);
      SetLength(WindowSize, 1);
      (*fDBData.dAgreementWindowOtlivSizePosition.DataSet.First();
      while(fDBData.dAgreementWindowOtlivSizePosition.DataSet.Eof = False)do
      begin
        CurRec := fDBData.dAgreementWindowOtlivSizePosition.DataSet.RecNo;
        fDBData.dSizePosition.DataSet.Filtered := False;
        fDBData.dSizePosition.DataSet.Filter := Format('SizePositionId = %s', [fDBData.dAgreementWindowSizePosition.DataSet.FieldByName('SizePositionId').AsString]);
        fDBData.dSizePosition.DataSet.Filtered := True;
        WindowSize[CurRec-1].xpos := fDBData.dSizePosition.DataSet.FieldByName('xpos').AsInteger;
        WindowSize[CurRec-1].ox := fDBData.dSizePosition.DataSet.FieldByName('ox').AsBoolean;
        WindowSize[CurRec-1].ypos := fDBData.dSizePosition.DataSet.FieldByName('ypos').AsInteger;
        WindowSize[CurRec-1].oy := fDBData.dSizePosition.DataSet.FieldByName('oy').AsBoolean;
        WindowSize[CurRec-1].angle := fDBData.dSizePosition.DataSet.FieldByName('angle').AsInteger;
        WindowSize[CurRec-1].text := fDBData.dAgreementWindowOtlivSizePosition.DataSet.FieldByName('value').AsString;
        fDBData.dSizePosition.DataSet.Filtered := False;
        fDBData.dSizePosition.DataSet.Filter := '';
        fDBData.dAgreementWindowOtlivSizePosition.DataSet.Next();
      end;//end while*)
        WindowSize[0].xpos := 0;
        WindowSize[0].ox := false;
        WindowSize[0].ypos := 0;
        WindowSize[0].oy := false;
        WindowSize[0].angle := 0;
        WindowSize[0].text := fDBData.dAgreementWindowOtlivSizePosition.DataSet.FieldByName('value').AsString;
      //WindowSize[length(WindowSize)-1].text := WindowSize[length(WindowSize)-1].text + ' ('+fDBData.dAgreementWindowOtliv.DataSet.FieldByName('amount').AsString+')';
    end;//end if
    if(cmybitmap.Width < 215)then cmybitmap.Width := 215;
    HImageTools.DrawRotateText(amybitmap, bmybitmap, WindowSize);
    cmybitmap.Height := cmybitmap.Height + bmybitmap.Height;
    cmybitmap.Canvas.Draw(0, cmybitmap.Height - bmybitmap.Height, bmybitmap);
    amybitmap.Free();
    bmybitmap.Free();
  end;//end if
  HImageTools.DefBitmapSizeIfEmpty(cmybitmap);
  Result := cmybitmap;
end;

function TfDBData.GetAllWindowImage():TBitmap;
var
  hResult: TBitmap;
begin
  hResult := TBitmap.Create;
  if(not dAgreementWindow.DataSet.IsEmpty) then
  begin
    dAgreementWindow.DataSet.First();
    while(not dAgreementWindow.DataSet.Eof) do
    begin
      HImageTools.SplitImageHor(GetWindowImage(), hResult);
      dAgreementWindow.DataSet.Next();
    end;
  end;
  HImageTools.DefBitmapSizeIfEmpty(hResult);
  Result := hResult;
end;

function TfDBData.GetAllOtlivImage():TBitmap;
var
  hResult: TBitmap;
begin
  hResult := TBitmap.Create;
  if(not dAgreementWindow.DataSet.IsEmpty) then
  begin
    dAgreementWindow.DataSet.First;
    while(not dAgreementWindow.DataSet.Eof) do
    begin
      if(not dAgreementWindowOtliv.DataSet.IsEmpty) then
      begin
        dAgreementWindowOtliv.DataSet.First();
        while(not dAgreementWindowOtliv.DataSet.Eof) do
        begin
          HImageTools.SplitImageVer(GetOtlivImage(), hResult);
          dAgreementWindowOtliv.DataSet.Next();
        end;
      end;
      dAgreementWindow.DataSet.Next();
    end;
  end;
  HImageTools.DefBitmapSizeIfEmpty(hResult);
  Result := hResult;
end;

function TfDBData.GetAllPodokonnik():String;
var
  hResult: String;
begin
  hResult := '';
  if(not dAgreementWindow.DataSet.IsEmpty) then
  begin
    dAgreementWindow.DataSet.First();
    while(not dAgreementWindow.DataSet.Eof) do
    begin
      if(not dAgreementWindowMaterial.DataSet.IsEmpty) then
      begin
        dAgreementWindowMaterial.DataSet.First();
        while(not dAgreementWindowMaterial.DataSet.Eof) do
        begin
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := Format('MaterialId = %s', [dAgreementWindowMaterial.DataSet.FieldByName('MaterialId').AsString]);
          dMaterial.DataSet.Filtered := True;
          if(dMaterial.DataSet.FieldByName('IsPodokonnik').AsBoolean)then
          begin
            hResult := hResult + dMaterial.DataSet.FieldByName('Name').AsString + ' ' + dAgreementWindowMaterial.DataSet.FieldByName('Value').AsString + #13;
          end;
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := '';
          dAgreementWindowMaterial.DataSet.Next();
        end;
      end;
      dAgreementWindow.DataSet.Next();
    end;
  end;
  Result := hResult;
end;

function TfDBData.GetAllOtliv():String;
var
  hResult: String;
begin
  hResult := '';
  if(not dAgreementWindow.DataSet.IsEmpty) then
  begin
    dAgreementWindow.DataSet.First();
    while(not dAgreementWindow.DataSet.Eof) do
    begin
      if(not dAgreementWindowMaterial.DataSet.IsEmpty) then
      begin
        dAgreementWindowMaterial.DataSet.First();
        while(not dAgreementWindowMaterial.DataSet.Eof) do
        begin
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := Format('MaterialId = %s', [dAgreementWindowMaterial.DataSet.FieldByName('MaterialId').AsString]);
          dMaterial.DataSet.Filtered := True;
          if(dMaterial.DataSet.FieldByName('IsOtliv').AsBoolean) then
          begin
            hResult := hResult + dMaterial.DataSet.FieldByName('Name').AsString + ' ' + dAgreementWindowMaterial.DataSet.FieldByName('Value').AsString + #13;
          end;
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := '';
          dAgreementWindowMaterial.DataSet.Next();
        end;
      end;
      dAgreementWindow.DataSet.Next();
    end;
  end;
  Result := hResult;
end;

function TfDBData.GetWindowMaterial(IsAllMaterial: Boolean):String;

type TMaterial = record
  Name: String[25];
  Value: String[25];
  IsMaterial: Boolean;
end;

var
  hResult: String;
  AMaterial: array of TMaterial;
  I: Integer;
  IsMaterialFound: Boolean;
begin
  hResult := '';
  if(not dAgreementWindow.DataSet.IsEmpty) then
  begin
    dAgreementWindow.DataSet.First();
    while(not dAgreementWindow.DataSet.Eof) do
    begin
      if(not dAgreementWindowMaterial.DataSet.IsEmpty) then
      begin
        dAgreementWindowMaterial.DataSet.First();
        while(not dAgreementWindowMaterial.DataSet.Eof) do
        begin
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := Format('MaterialId = %s', [dAgreementWindowMaterial.DataSet.FieldByName('MaterialId').AsString]);
          dMaterial.DataSet.Filtered := True;
          IsMaterialFound := False;
          if(Length(AMaterial) = 0) then
          begin
            SetLength(AMaterial, 1);
            AMaterial[0].Name := dMaterial.DataSet.FieldByName('Name').AsString;
            AMaterial[0].Value := dAgreementWindowMaterial.DataSet.FieldByName('Value').AsString;
            AMaterial[0].IsMaterial := dMaterial.DataSet.FieldByName('IsMaterial').AsBoolean;
            IsMaterialFound := True;
          end;
          if(not IsMaterialFound)then
          begin
            for I := Low(AMaterial) to High(AMaterial) do
            begin
              if((AMaterial[I].Name = dMaterial.DataSet.FieldByName('Name').AsString)and(dMaterial.DataSet.FieldByName('IsMaterial').AsBoolean))then
              begin
                if({(ConvertToInt(AMaterial[I].Value) > 0)and}(ConvertToInt(dAgreementWindowMaterial.DataSet.FieldByName('Value').AsString) > 0)) then
                begin
                  AMaterial[I].Value := IntToStr(ConvertToInt(AMaterial[I].Value) + dAgreementWindowMaterial.DataSet.FieldByName('Value').AsInteger);
                  IsMaterialFound := True;
                  Break;
                end;
              end;
            end;
          end;
          if(not IsMaterialFound)then
          begin
            SetLength(AMaterial, Length(AMaterial)+1);
            AMaterial[Length(AMaterial)-1].Name := dMaterial.DataSet.FieldByName('Name').AsString;
            AMaterial[Length(AMaterial)-1].Value := dAgreementWindowMaterial.DataSet.FieldByName('Value').AsString;
            AMaterial[Length(AMaterial)-1].IsMaterial := dMaterial.DataSet.FieldByName('IsMaterial').AsBoolean;
          end;
          dMaterial.DataSet.Filtered := False;
          dMaterial.DataSet.Filter := '';
          dAgreementWindowMaterial.DataSet.Next();
        end;
      end;
      dAgreementWindow.DataSet.Next();
    end;
  end;
  if(Length(AMaterial) > 0)then
  begin
    for I := Low(AMaterial) to High(AMaterial) do
    begin
      if(IsAllMaterial)then
      begin
        hResult := hResult + Format('%s %s'+#13, [AMaterial[I].Name, AMaterial[I].Value]);
      end else
      begin
        if(AMaterial[I].IsMaterial)then
        begin
          hResult := hResult + Format('%s %s'+#13, [AMaterial[I].Name, AMaterial[I].Value]);
        end;
      end;
    end;
  end;
  Result := hResult;
end;

function TfDBData.GetAgreementPayment():Real;
var
  hResult: Real;
begin
  hResult := 0;
  if(not dAgreementPayment.DataSet.IsEmpty)then
  begin
    dAgreementPayment.DataSet.First();
    while(not dAgreementPayment.DataSet.Eof)do
    begin
      hResult := hResult + dAgreementPayment.DataSet.FieldByName('summa').AsFloat;
      dAgreementPayment.DataSet.Next();
    end;
  end;
  if(hResult > 0) then hResult := dAgreement.DataSet.FieldByName('Summa').AsFloat - hResult;
  Result := hResult;
end;

procedure TfDBData.GetResults();
begin
  if((qApartmentPlanning.Active)and(qWindowGroup.Active))then
  begin
    if((not dApartmentPlanning.DataSet.IsEmpty)and(not dWindowGroup.DataSet.IsEmpty))then
    begin
      qApartmentWindowMaterial.Active := False;
      qApartmentWindowMaterial.ParamByName('IDApartmentPlanning').Value := dApartmentPlanning.DataSet.FieldByName('ApartmentPlanningId').AsString;
      qApartmentWindowMaterial.ParamByName('IDWindowGroup').Value := dWindowGroup.DataSet.FieldByName('WindowGroupId').AsString;
      qApartmentWindowMaterial.Active := True;
      if(qWorkmanGroup.Active)then
      begin
        if(not dWorkmanGroup.DataSet.IsEmpty)then
        begin
          qApartmentWindowWorkmanSalary.Active := False;
          qApartmentWindowWorkmanSalary.ParamByName('IDApartmentPlanning').Value := dApartmentPlanning.DataSet.FieldByName('ApartmentPlanningId').AsString;
          qApartmentWindowWorkmanSalary.ParamByName('IDWindowGroup').Value := dWindowGroup.DataSet.FieldByName('WindowGroupId').AsString;
          qApartmentWindowWorkmanSalary.ParamByName('IDWorkmanGroup').Value := dWorkmanGroup.DataSet.FieldByName('WorkmanGroupId').AsString;
          qApartmentWindowWorkmanSalary.Active := True;
        end;
      end;
    end;
  end;
end;

procedure TfDBData.qAgreementWindowMaterialAfterPost(DataSet: TDataSet);
var
  is_find: Boolean;
begin
  dWindowOption.DataSet.First();
  fWindowImage.Image1.Picture.Bitmap.Assign(HImageTools.GetBitmapByID(dWindow, 'WindowId', dAgreementWindow.DataSet.FieldByName('WindowId').AsString, 'WindowImage'));
  while(dWindowOption.DataSet.Eof = False)do
  begin
    is_find := False;
    if(DataSet.FieldByName('MaterialId').AsString = dWindowOption.DataSet.FieldByName('MaterialId').AsString)then
    begin
      if(dAgreementWindowOption.DataSet.IsEmpty = False)then
      begin
        while(dAgreementWindowOption.DataSet.Eof = False)do
        begin
          if(dAgreementWindowOption.DataSet.FieldByName('MaterialId').AsString = DataSet.FieldByName('MaterialId').AsString)then
          begin
            is_find := True;
            break;
          end;
          dAgreementWindowOption.DataSet.Next();
        end;
        if(is_find = False)then
        begin
          fWindowImage.ShowModal();
          while((fWindowImage.WindowOptionDoit = False)and((fWindowImage.WindowOptionXPos = 0)and(fWindowImage.WindowOptionYPos = 0)))do
          begin
            //Application.ProcessMessages();
          end;
          fDBCommand.qAddAgreementWindowOption.ParamByName('IDAgreementWindow').Value := dAgreementWindow.DataSet.FieldByName('AgreementWindowId').AsString;
          fDBCommand.qAddAgreementWindowOption.ParamByName('IDAgreementWindowMaterial').Value := dAgreementWindowMaterial.DataSet.FieldByName('AgreementWindowMaterialId').AsString;
          fDBCommand.qAddAgreementWindowOption.ParamByName('IDWindowOption').Value := dWindowOption.DataSet.FieldByName('WindowOptionId').AsString;
          fDBCommand.qAddAgreementWindowOption.ParamByName('XPos').Value := fWindowImage.WindowOptionXPos;
          fDBCommand.qAddAgreementWindowOption.ParamByName('YPos').Value := fWindowImage.WindowOptionYPos;
          fDBCommand.qAddAgreementWindowOption.Execute();
        end;
      end
      else
      begin
        fWindowImage.ShowModal();
        while((fWindowImage.WindowOptionDoit = False)and((fWindowImage.WindowOptionXPos = 0)and(fWindowImage.WindowOptionYPos = 0)))do
        begin
          //Application.ProcessMessages();
        end;
        fDBCommand.qAddAgreementWindowOption.ParamByName('IDAgreementWindow').Value := dAgreementWindow.DataSet.FieldByName('AgreementWindowId').AsString;
        fDBCommand.qAddAgreementWindowOption.ParamByName('IDAgreementWindowMaterial').Value := dAgreementWindowMaterial.DataSet.FieldByName('AgreementWindowMaterialId').AsString;
        fDBCommand.qAddAgreementWindowOption.ParamByName('IDWindowOption').Value := dWindowOption.DataSet.FieldByName('WindowOptionId').AsString;
        fDBCommand.qAddAgreementWindowOption.ParamByName('XPos').Value := fWindowImage.WindowOptionXPos;
        fDBCommand.qAddAgreementWindowOption.ParamByName('YPos').Value := fWindowImage.WindowOptionYPos;
        fDBCommand.qAddAgreementWindowOption.Execute();
      end;
      fWindowImage.WindowOptionXPos := 0;
      fWindowImage.WindowOptionYPos := 0;
      fWindowImage.WindowOptionDoit := False;
    end;
    dWindowOption.DataSet.Next();
  end;
end;

procedure TfDBData.qAgreementWindowMaterialBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('AgreementWindowId').Value := dAgreementWindow.DataSet.FieldByName('AgreementWindowId').AsString;
end;

procedure TfDBData.qAgreementWindowMaterialAfterEdit(DataSet: TDataSet);
begin
  DataSet.Post();
end;

procedure TfDBData.qWindowGroupSizePositionBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('WindowGroupId').Value := dWindowGroup.DataSet.FieldByName('WindowGroupId').AsString;
end;

procedure TfDBData.qApartmentPlanningAfterScroll(DataSet: TDataSet);
begin
  GetResults();
end;

procedure TfDBData.qApartmentWindowMaterialBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('ApartmentPlanningId').Value := dApartmentPlanning.DataSet.FieldByName('ApartmentPlanningId').AsString;
  DataSet.FieldByName('WindowGroupId').Value := dWindowGroup.DataSet.FieldByName('WindowGroupId').AsString;
end;

procedure TfDBData.qWorkmanGroupAfterScroll(DataSet: TDataSet);
begin
  GetResults();
end;

procedure TfDBData.qWorkmanSalaryAfterScroll(DataSet: TDataSet);
begin
  if(DataSet.IsEmpty = False)then
  begin
    qWorkmanConsuption.Active := False;
    qWorkmanConsuption.ParamByName('IDWorkman').Value := DataSet.FieldByName('WorkmanId').AsString;
    qWorkmanConsuption.Active := True;
    qWorkmanSalaryDetail.Active := False;
    qWorkmanSalaryDetail.ParamByName('IDWorkman').Value := DataSet.FieldByName('WorkmanId').AsString;
    qWorkmanSalaryDetail.Active := True;
  end;
end;

procedure TfDBData.qWorkmanConsuptionBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('WorkmanId').Value := dWorkmanSalary.DataSet.FieldByName('WorkmanId').AsString;
end;

procedure TfDBData.qOtlivGroupAfterScroll(DataSet: TDataSet);
begin
  if(DataSet.IsEmpty = False)then
  begin
    qOtlivGroupSizePosition.Active := False;
    qOtlivGroupSizePosition.ParamByName('IDOtlivGroup').Value := DataSet.FieldByName('OtlivGroupId').AsString;
    qOtlivGroupSizePosition.Active := True;
  end;
end;

procedure TfDBData.qOtlivGroupSizePositionBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('OtlivGroupId').Value := dOtlivGroup.DataSet.FieldByName('OtlivGroupId').AsInteger;
end;

procedure TfDBData.qApartmentWindowWorkmanSalaryBeforePost(
  DataSet: TDataSet);
begin
  DataSet.FieldByName('ApartmentPlanningId').Value := dApartmentPlanning.DataSet.FieldByName('ApartmentPlanningId').AsString;
  DataSet.FieldByName('WorkmanGroupId').Value := dWorkmanGroup.DataSet.FieldByName('WorkmanGroupId').AsString;
  DataSet.FieldByName('WindowGroupId').Value := dWindowGroup.DataSet.FieldByName('WindowGroupId').AsString;
end;

procedure TfDBData.qAgreementAfterScroll(DataSet: TDataSet);
begin
  qAgreementWindow.Active := False;
  qAgreementPayment.Active := False;
  qAgreementWorkman.Active := False;
  qAgreementOperationParamColor.Active := False;
  if((DataSet.IsEmpty = False)and(DataSet.State <> dsEdit)and(DataSet.State <> dsInsert))then
  begin
    qAgreementWindow.ParamByName('IDAgreement').Value := DataSet.FieldByName('AgreementId').AsString;
    qAgreementWindow.Active := True;
    qAgreementPayment.ParamByName('IDAgreement').Value := DataSet.FieldByName('AgreementId').AsString;
    qAgreementPayment.Active := True;
    qAgreementWorkman.ParamByName('IDAgreement').Value := DataSet.FieldByName('AgreementId').AsString;
    qAgreementWorkman.Active := True;
    qAgreementOperationParamColor.ParamByName('IDAgreement').Value := DataSet.FieldByName('AgreementId').AsInteger;
    qAgreementOperationParamColor.Active := True;
  end;
end;

procedure TfDBData.qAgreementWorkmanBeforePost(DataSet: TDataSet);
begin
    DataSet.FieldByName('AgreementId').Value := dAgreement.DataSet.FieldByName('AgreementId').AsString;
end;

procedure TfDBData.qAgreementWindowAfterScroll(DataSet: TDataSet);
begin
  qAgreementWindowSizePosition.Active := False;
  qAgreementWindowMaterial.Active := False;
  qAgreementWindowOtliv.Active := False;
  qAgreementWindowOption.Active := False;
  qAgreementWindowOperationParamColor.Active := False;
  if((DataSet.IsEmpty = False)and(DataSet.State <> dsEdit)and(DataSet.State <> dsInsert))then
  begin
    qAgreementWindowSizePosition.ParamByName('IDAgreementWindow').Value := DataSet.FieldByName('AgreementWindowId').AsInteger;
    qAgreementWindowSizePosition.Active := True;
    qAgreementWindowMaterial.ParamByName('IDAgreementWindow').Value := DataSet.FieldByName('AgreementWindowId').AsInteger;
    qAgreementWindowMaterial.Active := True;
    qAgreementWindowOtliv.ParamByName('IDAgreementWindow').Value := DataSet.FieldByName('AgreementWindowId').AsInteger;
    qAgreementWindowOtliv.Active := True;
    qAgreementWindowOption.ParamByName('IDAgreementWindow').Value := DataSet.FieldByName('AgreementWindowId').AsInteger;
    qAgreementWindowOption.Active := True;
    qAgreementWindowOperationParamColor.ParamByName('IDAgreementWindow').Value := DataSet.FieldByName('AgreementWindowId').AsInteger;
    qAgreementWindowOperationParamColor.Active := True;
  end;
end;

procedure TfDBData.qWindowGroupAfterScroll(DataSet: TDataSet);
begin
  GetResults();
  qWindowGroupSizePosition.Active := False;
  qWindowGroupSizePosition.ParamByName('IDWindowGroup').Value := DataSet.FieldByName('WindowGroupId').AsInteger;
  qWindowGroupSizePosition.Active := True;
end;

function TfDBData.frxMainReportUserFunction(const MethodName: String;
  var Params: Variant): Variant;
begin
  if (MethodName = 'GETALLWINDOWIMAGE') then
    TfrxPictureView(frxMainReport.FindObject('vWindowImage')).Picture.Bitmap := GETALLWINDOWIMAGE();
  if (MethodName = 'GETALLOTLIVIMAGE') then
    TfrxPictureView(frxMainReport.FindObject('vOtlivImage')).Picture.Bitmap := GETALLOTLIVIMAGE();
  if (MethodName = 'GETALLPODOKONNIK') then
    TfrxMemoView(frxMainReport.FindObject('vPodokonnik')).Text := GETALLPODOKONNIK();
  if (MethodName = 'GETALLOTLIV') then
    TfrxMemoView(frxMainReport.FindObject('vOtliv')).Text := GETALLOTLIV();
  if (MethodName = 'GETWINDOWMATERIAL') then
    TfrxMemoView(frxMainReport.FindObject('vMaterial')).Text := GETWINDOWMATERIAL(False);
  if (MethodName = 'GETALLWINDOWMATERIAL') then
    TfrxMemoView(frxMainReport.FindObject('vMaterial')).Text := GETWINDOWMATERIAL(True);
  if (MethodName = 'GETAGREEMENTPAYMENT') then
    TfrxMemoView(frxMainReport.FindObject('vAgreementSumma')).Text := FloatToStr(GETAGREEMENTPAYMENT());
end;

procedure TfDBData.qAgreementPaymentBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('AgreementId').Value := dAgreement.DataSet.FieldByName('AgreementId').AsInteger; 
end;

procedure TfDBData.DataModuleCreate(Sender: TObject);
begin
  frxMainReport.AddFunction('procedure GETALLWINDOWIMAGE();');
  frxMainReport.AddFunction('procedure GETALLOTLIVIMAGE();');
  frxMainReport.AddFunction('procedure GETALLPODOKONNIK();');
  frxMainReport.AddFunction('procedure GETALLOTLIV();');
  frxMainReport.AddFunction('procedure GETWINDOWMATERIAL();');
  frxMainReport.AddFunction('procedure GETALLWINDOWMATERIAL();');
  frxMainReport.AddFunction('procedure GETAGREEMENTPAYMENT();');
end;

procedure TfDBData.qAgreementOperationParamColorBeforePost(
  DataSet: TDataSet);
begin
  DataSet.FieldByName('AgreementId').Value := dAgreement.DataSet.FieldByName('AgreementId').AsInteger;
end;

procedure TfDBData.qAgreementWindowOperationParamColorBeforePost(
  DataSet: TDataSet);
begin
  DataSet.FieldByName('AgreementWindowId').Value := dAgreementWindow.DataSet.FieldByName('AgreementWindowId').AsInteger;
end;

procedure TfDBData.qAgreementWindowOtlivAfterScroll(DataSet: TDataSet);
begin
  qAgreementWindowOtlivSizePosition.Active := False;
  if((DataSet.IsEmpty = False)and(DataSet.State <> dsEdit)and(DataSet.State <> dsInsert))then
  begin
    qAgreementWindowOtlivSizePosition.ParamByName('IDAgreementWindowOtliv').Value := DataSet.FieldByName('AgreementWindowOtlivId').AsInteger;
    qAgreementWindowOtlivSizePosition.Active := True;
  end;
end;

procedure TfDBData.qAgreementWindowOtlivBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('AgreementWindowOtlivId').Value := dAgreementWindowOtliv.DataSet.FieldByName('AgreementWindowOtlivId').AsInteger;
end;

procedure TfDBData.frxMainReportProgressStart(Sender: TfrxReport;
  ProgressType: TfrxProgressType; Progress: Integer);
begin
  dAgreement.DataSet.Filtered := False;
  dAgreement.DataSet.Filter := 'IsSelected = True';
  dAgreement.DataSet.Filtered := True;
end;

procedure TfDBData.frxMainReportProgressStop(Sender: TfrxReport;
  ProgressType: TfrxProgressType; Progress: Integer);
begin
  dAgreement.DataSet.Filtered := False;
  dAgreement.DataSet.Filter := '';
end;

procedure TfDBData.qWorkmanBeforePost(DataSet: TDataSet);
begin
  DataSet.FieldByName('WorkmanGroupId').Value := dWorkmanGroup.DataSet.FieldByName('WorkmanGroupId').AsInteger;
end;

end.
  
